// 搜索引擎区块
var tabs = document.getElementsByClassName("sstab");
for (var i = 0; i < tabs.length; i++) {
  tabs[i].addEventListener("click", function () {
    var activeSstab = document.getElementsByClassName("active")[0];
    activeSstab.classList.remove("active");
    this.classList.add("active");
  });
}

function performSearch() {
  var activeSstab = document.getElementsByClassName("active")[0].id;
  var inputContent = document.querySelector("#search-input input").value;

  switch (activeSstab) {
    case "baidu-tab":
      window.location.href = "https://www.baidu.com/s?wd=" + inputContent;
      break;
    case "bing-tab":
      window.location.href = "https://www.bing.com/search?q=" + inputContent;
      break;
    case "360-tab":
      window.location.href = "https://www.so.com/s?q=" + inputContent;
      break;
    case "sogou-tab":
      window.location.href = "https://www.sogou.com/web?query=" + inputContent;
      break;
    case "wanfeng-tab":
      window.location.href = "https://www.a754.com/?s=" + inputContent;
      break;
    case "GT-tab":
      window.location.href = "https://search.gitee.com/?skin=rec&type=repository&q=" + inputContent;
      break;
    case "Bzhan-tab":
      window.location.href = "https://search.bilibili.com/all?keyword=" + inputContent;
      break;
    case "wuzhui-tab":
      window.location.href = "https://www.wuzhuiso.com/s?ie=utf-8&fr=none&src=360sou_newhome&q=" + inputContent;
      break;

    default:
      break;
  }

  // 在新标签页中打开链接
  window.open(searchLink, '_blank');
}


// 移动端侧边栏
function openNav() {
  document.getElementById("mySidebar").style.left = "0"; // 修改此处为 left
}

function closeNav() {
  document.getElementById("mySidebar").style.left = "-100vw"; // 修改此处为 -250px
}

// 置顶按钮
var scrollToTopBtn = document.getElementById("scroll-to-top");

// 监听页面滚动事件
window.addEventListener("scroll", function () {
  // 获取页面滚动的垂直距离
  var scrollY = window.scrollY || document.documentElement.scrollTop;

  // 如果滚动距离超过500px，显示按钮，否则隐藏
  if (scrollY >= 200) {
    scrollToTopBtn.style.display = "block";
  } else {
    scrollToTopBtn.style.display = "none";
  }
});

scrollToTopBtn.addEventListener("click", function () {
  // 平滑滚动到顶部
  window.scrollTo({
    top: 0,
    behavior: "smooth"
  });
});

// 获取按钮和所有a元素、button元素和h3元素
var switchBtn = document.getElementById("mode-switch");
var elements = document.querySelectorAll("a, button, h3");

// 添加点击事件监听器
switchBtn.addEventListener("click", function () {
  // 切换所有元素的字体颜色
  elements.forEach(function (element) {
    if (element.style.color === "white") {
      element.style.color = "black";
    } else {
      element.style.color = "white";
    }
  });
});
// 悬浮按钮
window.addEventListener('DOMContentLoaded', (event) => {
  const modeSwitchButton = document.getElementById('mode-switch');
  const moonIcon = document.getElementById('moon-icon');
  const sunIcon = document.getElementById('sun-icon');
  const headbs = document.getElementById('headerbs');
  const flbsx = document.getElementById('flbs');
  const cblzcbsx = document.getElementById('cblzcbs');
  let isDayMode = true;

  modeSwitchButton.addEventListener('click', () => {
    isDayMode = !isDayMode;

    if (isDayMode) {
      moonIcon.style.display = 'inline-block';
      sunIcon.style.display = 'none';
      // 执行白天模式切换的操作
      document.body.style.backgroundColor = '#ebecf0';  // 举例：将背景颜色切换为白色

      document.getElementById('flbs');
      flbsx.style.backgroundColor = '#ebecf0';

      document.getElementById('headerbs');
      headbs.style.backgroundColor = '#fff';

      document.getElementById('cblzcbs');
      cblzcbsx.style.backgroundColor = '#ebecf0';

    } else {
      moonIcon.style.display = 'none';
      sunIcon.style.display = 'inline-block';
      // 执行夜间模式切换的操作
      document.body.style.backgroundColor = '#000000';  // 举例：将背景颜色切换为黑色

      document.getElementById('flbs');
      flbsx.style.backgroundColor = '#000000';

      document.getElementById('sun-icon');
      sunIcon.style.boxShadow = '2px 2px 10px #fff';

      document.getElementById('headerbs');
      headbs.style.backgroundColor = '#000000';

      document.getElementById('cblzcbs');
      cblzcbsx.style.backgroundColor = '#000000';
    }
  });
});




// 在main-list内滚动
document.addEventListener('DOMContentLoaded', function () {
  const links = document.querySelectorAll('.cebianlan a');

  for (let i = 0; i < links.length; i++) {
    links[i].addEventListener('click', function (event) {
      event.preventDefault();

      const targetId = this.getAttribute('href');
      const targetElement = document.querySelector(targetId);
      const containerElement = document.querySelector('.main-list');

      containerElement.scrollTop = targetElement.offsetTop - containerElement.offsetTop;
    });
  }
});

//动态标题
var OriginTitile = document.title,
  titleTime;
document.addEventListener("visibilitychange",
  function () {
    if (document.hidden) {
      document.title = "你别走吖 Σ(っ °Д °;)っ";
      clearTimeout(titleTime)
    } else {
      document.title = "(/≧▽≦/)你又回来啦！ ";
      titleTime = setTimeout(function () {
        document.title = OriginTitile
      },
        2000)
    }
  });

// 获取按钮元素
const button = document.getElementById("x");
// 获取目标div元素
const div = document.querySelector(".fl");
//获取上下图标
const svgIcon = document.getElementById('sx');
// 添加鼠标移入事件监听器
button.addEventListener("mouseenter", function () {
  div.style.transition = '5s';
  // 修改div的display属性为block
  div.style.display = "block";
  //使svg图标垂直翻转
  svgIcon.style.transform = 'scaleY(1)';
});

// 添加鼠标移出事件监听器（如果需要隐藏div）
button.addEventListener("mouseleave", function () {
  // 修改div的display属性为none
  div.style.display = "none";
  //使svg图标恢复默认样式
  svgIcon.style.transform = 'scaleY(-1)';
});
// 鼠标移入div时保持显示且上下标保持垂直翻转
div.addEventListener("mouseenter", () => {
  div.style.display = "block";
  svgIcon.style.transform = 'scaleY(1)';
});

// 鼠标移出div时隐藏且上下标恢复默认样式
div.addEventListener("mouseleave", () => {
  div.style.display = "none";
  svgIcon.style.transform = 'scaleY(-1)';
});

// 获取所有类名为"tab"的div元素
var tabDivs = document.getElementsByClassName("tab");

// 遍历每个div元素
for (var i = 0; i < tabDivs.length; i++) {
  var tabDiv = tabDivs[i];

  // 找到div内的第一个button按钮
  var firstButton = tabDiv.querySelector("button");

  // 设置按钮为激活状态
  firstButton.classList.add("active");
}

function openTab(evt, tabName) {
  // 获取当前节点的父节点 -> 找到 class = tabcontent的节点
  var tabcontent = $(evt.currentTarget).parent().nextAll('.tabcontent');
  // 全部隐藏
  tabcontent.hide();
  // 获取除当前节点外的所有兄弟节点
  $(evt.currentTarget).siblings().removeClass('active');
  $(evt.currentTarget).addClass('active');

  // 以下为新增内容
  // 获取当前节点的父节点 -> 找到目标节点
  let targetTab = $(evt.currentTarget).parent().nextAll('#' + tabName);
  targetTab.show();
}